package com.lovo.invoicing.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.invoicing.bean.PageBean;
import com.lovo.invoicing.bean.SaleInfoBean;
import com.lovo.invoicing.bean.vo.*;
import com.lovo.invoicing.service.ISaleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/saleInfo")
public class SaleInfoController {
    @Autowired
    ISaleInfoService saleInfoService;
    @RequestMapping("/findSaleInfoByPage")
    public IPage<SaleInfoBean> findSaleInfoByPage(@RequestBody PageBean<SaleInfoBean> pageBean){
        return saleInfoService.querySaleInfoByPage(pageBean);
    }
    @RequestMapping("/findBySaleInfoId")
    public List<SaleInfoSaleBean> findBySaleInfoId(int saleInfoId){
        return saleInfoService.queryBySaleInfoId(saleInfoId);
    }

    //销售类别统计
    @RequestMapping("/findTypeTotal")
    public List<ValueNameBean> findTypeTotal(String startDate,String endDate){
        List<SaleTypeBean> list = saleInfoService.queryTypeTotal(startDate,endDate);
        List<ValueNameBean> vnList =new ArrayList<>();
        for (SaleTypeBean saleTypeBean : list) {
            ValueNameBean vn =new ValueNameBean();
            vn.setValue(saleTypeBean.getTotalPrice());
            if (saleTypeBean.getGoodsType()==1){
                vn.setName("电脑产品");
            }else if(saleTypeBean.getGoodsType()==2){
                vn.setName("安防产品");
            }else {
                vn.setName("办公耗材");
            }
            vnList.add(vn);
        }
        return vnList;
    }

    //销售产品统计
    @RequestMapping("/findNameTotal")
    public List<ValueNameBean> findNameTotal(String startDate, String endDate){
        List<SaleGoodsBean> list = saleInfoService.queryNameTotal(startDate, endDate);
        List<ValueNameBean> vnList =new ArrayList<>();
        for (SaleGoodsBean saleGoodsBean : list) {
            ValueNameBean vn =new ValueNameBean();
            vn.setValue(saleGoodsBean.getTotalPrice());
            vn.setName(saleGoodsBean.getGoodsName());
            vnList.add(vn);
        }
        return vnList;
    }

    //月销售统计
    @RequestMapping("/findByMonth")
    public MonthBean findByMonth(String saleYear) {
        MonthBean monthBean = new MonthBean();
        monthBean.setMonthList(saleInfoService.queryMonthsBySaleYear(saleYear));
        monthBean.setComputerList(saleInfoService.queryComputerPrices(saleYear));
        monthBean.setSecurityList(saleInfoService.querySecurityPrices(saleYear));
        monthBean.setWorkList(saleInfoService.queryWorkPrices(saleYear));
        return monthBean;
    }

    @RequestMapping("/findAllYears")
    public List<String> findAllYears(){
        return saleInfoService.queryAllSaleYears();
    }

    @RequestMapping("/findComputerPrices")
    public List<Double> findComputerPrices(String saleYear){
        return saleInfoService.queryComputerPrices(saleYear);
    }

    @RequestMapping("/findSecurityPrices")
    public List<Double> findSecurityPrices(String saleYear){
        return saleInfoService.querySecurityPrices(saleYear);
    }
    @RequestMapping("/findWorkPrices")
    public List<Double> findWorkPrices(String saleYear){
        return saleInfoService.queryWorkPrices(saleYear);
    }
}
